﻿using System;
using System.IO;
using System.Linq;
using FileHelpers;
using Model;
using Model.Static;

namespace Command.Commands
{
    public class SyncAchievementIconsCommand : BaseCommand<Options>
    {
        private readonly IRepository _repository;

        public SyncAchievementIconsCommand(IRepository repository)
        {
            _repository = repository;
        }

        protected override void InternalExecute(Options options)
        {
            var file = Path.Combine(Environment.CurrentDirectory, "Static\\Data\\AchievementIcon.csv");
            var rows = new FileHelperEngine<AchievementIconRecord> { Options = { IgnoreFirstLines = 1 } }
                .ReadFile(file)
                .OrderBy(x => x.ExternalAchievementKey)
                .ToArray();
            foreach (var row in rows)
            {
                var theRow = row;
                var achievement = _repository.Query<Achievement>().FirstOrDefault(x => x.ExternalKey == theRow.ExternalAchievementKey);
                if (achievement != null)
                {
                    if (achievement.Icon != theRow.Name)
                    {
                        achievement.Icon = theRow.Name;
                        Console.WriteLine("Assigned New Icon: {0}", achievement.Icon);
                        _repository.Update(achievement);
                    }
                }
            }
        }
    }
}
